// MPC3 is a general wrap class of Matlab engine. It pass the feedback
// to Matlab and return the calculation of Matlab back to C. Detailed 
// implementation of MPC is done in Matlab.
// 
// Author: Xinyu Zhang (zxy1256@gmail.com)
// Date: 2009.08.25

#ifndef MPC_MATLAB_T_H
#define MPC_MATLAB_T_H
#include "MPC_Base_T.h"
#include "C:\Program Files\MATLAB\R2007a\extern\include\engine.h"
#include <iomanip>

class MPC_Matlab_T:public MPC_Base
{
private:
	Engine *ep;
	
	// INPUTS
	mxArray *mxAlphas;
	mxArray *mxBetas;
	mxArray *mxHt;
	double  *pHt;
	mxArray *mxG_dens;
	double  *pG_dens;
	mxArray *mxW0;
	double  *pW0;
	mxArray *mxTime;
	double  *pTime;
	
	// SETTINGS
	mxArray *mxLb;
	double	*pLb;
	mxArray	*mxUb;
	double	*pUb;
	mxArray *mxD_set;
	mxArray *mxFact_D;
	mxArray *mxR2_set;
	mxArray *mxFact_R2;
	mxArray *mxHt_set;
	mxArray *mxFact_H;
	mxArray *mxVarR2_set;
	mxArray *mxFact_VarR2;
	mxArray *mxM2_set;
	mxArray *mxM2_fact;
	mxArray *mxStep_num;
	mxArray *mxMode;
	mxArray *mxdt;
	mxArray *mxT_LOW;
	mxArray *mxT_HIGH;
	mxArray *mxRT_T;
	mxArray *mxT_end;
	mxArray *mxControllerTypeID;

	char*	MPCFunctionName;

	int  FlagResult;
	char MatlabOutputBuffer[5120];
	ofstream fout_MPC3;

	void initialization();
public:
	MPC_Matlab_T(   double	iDens_set,
					double	iFact_D, 
					double	iRough2_set,
					double	iFact_R2, 
					double	iHt_set,	
					double	iFact_H, 
					double  ivarR2_set,
					double  iFact_varR2,
					double  iM2_set,
					double  iM2_fact,
					double	it_end,
					double	iT_ini = 500,
					double	iRT_T = 10,
					double	it_int_ctrl=1.0, 
					int		iStep_num=10,
					int		iRel_err=1,
					char*	iFileName = "MPC_Matlab_T.dat",
					int		iDetail = 0,
					int		iControllerID = 101,
					char*   iMPCFunctionName = "MPC_Matlab_T");
	~MPC_Matlab_T();
	virtual double  update(lattice_state iLState,const double P, const double T);
	virtual double  update2(const double time,const lattice_state iLState);
	virtual void    reset();
};
#endif

